package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.dto.MACD.TimeHorizonDto;
import com.mf.entity.WinRate;
import com.mf.mapper.WinRateMapper;
import com.mf.service.IWinRateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.utils.Result;
import com.mf.vo.FindWinRateVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.stream.IntStream;

/**
 * <p>
 * 胜率表 服务实现类
 * </p>
 *
 * @author mf
 * @since 2024-05-03
 */
@Service
public class WinRateServiceImpl extends ServiceImpl<WinRateMapper, WinRate> implements IWinRateService {
    @Resource
    private WinRateMapper winRateMapper;

    @Override
    public List<FindWinRateVo> findWinRate(TimeHorizonDto timeHorizon) {
        final LambdaQueryWrapper<WinRate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WinRate::getId, 1);
        final List<WinRate> winRateList = winRateMapper.selectList(queryWrapper);

        final List<FindWinRateVo> findWinRateVos = new ArrayList<>();
        for (WinRate winRate : winRateList) {
            if (winRate.getSuccessDate() != null && !winRate.getSuccessDate().equals("null")) {
                String[] dates = winRate.getSuccessDate().split(",");
                for (String dateStr : dates) {
                    try {
                        // 解析日期字符串为LocalDate对象
                        LocalDate date = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));

                        // 获取年份和月份
                        String year = date.getYear() + "";
                        // 获取月份
                        String month = date.getMonthValue() + "";

                        // 查找年份是否已经存在
                        OptionalInt yearIndex = findYearIndex(findWinRateVos, year);

                        if (yearIndex.isPresent()) {
                            // 年份已存在，获取对应的FindWinRateVo对象
                            FindWinRateVo yearFindWinRateVo = findWinRateVos.get(yearIndex.getAsInt());

                            System.out.println(yearFindWinRateVo.getChild().size());
                            // 查找月份是否已经存在
                            int monthIndex = findMonthIndex(yearFindWinRateVo.getChild(), month);

                            if (monthIndex != -1) {
                                yearFindWinRateVo.getChild().get(monthIndex).getSuccessDateArray().add(dateStr);
                                // 月份已存在，可以在此处添加处理逻辑，例如增加天数等
                            } else {
                                // 月份不存在，创建一个新的FindWinRateVo对象并添加到子列表中
                                final FindWinRateVo findWinRateVo1 = new FindWinRateVo();
                                findWinRateVo1.setDate(month);
                                findWinRateVo1.setSuccessDateArray(new ArrayList<>());
                                findWinRateVo1.getSuccessDateArray().add(dateStr);
                                System.out.println();
                                yearFindWinRateVo.getChild().add(findWinRateVo1);
                            }
                        } else {
                            // 年份不存在，创建一个新的FindWinRateVo对象并添加到列表中
                            // 假设FindWinRateVo有构造函数接受年
                            FindWinRateVo newVo = new FindWinRateVo();
                            newVo.setDate(year);
                            newVo.setSuccess(1);
                            newVo.setChild(new ArrayList<>());
                            // 初始化月份列表
                            final FindWinRateVo monthFindWinRateVo = new FindWinRateVo();
                            monthFindWinRateVo.setDate(month);
                            monthFindWinRateVo.setSuccessDateArray(new ArrayList<>());
                            monthFindWinRateVo.getSuccessDateArray().add(dateStr);
                            newVo.getChild().add(monthFindWinRateVo);
                            findWinRateVos.add(newVo);
                        }
                    } catch (DateTimeParseException e) {
                        // 处理日期解析异常
                        e.printStackTrace();
                    }
                }
            }
        }
        return findWinRateVos;
    }


    // 辅助方法：在FindWinRateVo列表中查找指定年份的索引
    private OptionalInt findYearIndex(List<FindWinRateVo> list, String year) {
        return IntStream.range(0, list.size())
                .filter(i -> list.get(i).getDate().equals(year))
                .findFirst();
    }

    private int findMonthIndex(List<FindWinRateVo> childList, String month) {
        for (int i = 0; i < childList.size(); i++) {
            if (childList.get(i).getDate().equals(month)) {
                return i;
            }
        }
        return -1;
    }
}
